package fetch

import (
	"bufio"
	"errors"
	"golang.org/x/net/html/charset"
	"golang.org/x/text/encoding"
	"golang.org/x/text/encoding/unicode"
	"golang.org/x/text/transform"
	"io/ioutil"
	"net/http"
)

func DetermineEncode(r *bufio.Reader)encoding.Encoding{
	bytes,err:=r.Peek(1024)
	if err!= nil {
		return unicode.UTF8
	}
	e,_,_:=charset.DetermineEncoding(bytes,"")
	return e
}
func Fetch(url string)([]byte,error){
	var content []byte
	resp,err:=http.Get(url)
	if err!=nil{
		return content,err
	}
	defer resp.Body.Close()
	if resp.StatusCode!=http.StatusOK{
		return content,errors.New("状态出错了")
	}
	newReader:=bufio.NewReader(resp.Body)
	e:=DetermineEncode(newReader)
	utf8Reader:=transform.NewReader(newReader,e.NewDecoder())
	return ioutil.ReadAll(utf8Reader)
}
